package com.hzy.oms.controller;

import com.hzy.config.LocalDataUtil;
import com.hzy.constant.Constants;
import com.hzy.oms.domain.bo.AftersaleBo;
import com.hzy.oms.domain.vo.AftersaleVo;
import com.hzy.oms.service.IAftersaleService;
import com.hzy.ums.domain.vo.MemberVo;
import com.hzy.utils.RedisExUtils;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright (C), 2022-2023
 * FileName: AftersaleController
 * Description: 售后
 * Author:   zengxin
 * Date:     2023/10/21 11:57
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/oms/aftersale")
public class AftersaleController {

    private final IAftersaleService aftersaleService;

    /***
     * 申请售后
     * @param bo
     * @return
     */
    @RepeatSubmit
    @PostMapping("/applyRefund")
    public R<String> applyRefund(@RequestBody @Validated AftersaleBo bo){
        String redisKey = "h5_oms_order_applyRefund_" + bo.getOrderId();
        String redisValue = bo.getOrderId() + "_" + System.currentTimeMillis();
        MemberVo member = (MemberVo) LocalDataUtil.getVar(Constants.MEMBER_INFO);
        try{
            bo.setMemberId(member.getId());
            RedisExUtils.lock(redisKey, redisValue, 60);
            return R.ok(aftersaleService.applyRefund(bo));
        }catch (Exception e){
            log.error("申请售后发生异常",e);
            throw new RuntimeException(e.getMessage());
        }finally {
            try {
                RedisExUtils.unLock(redisKey, redisValue);
            } catch (Exception e) {
                log.error("", e);
            }
        }
    }

    /***
     * 取消售后
     * @param orderId
     * @return
     */
    @GetMapping("/cancelRefund")
    public R<String> cancelRefund(Long orderId){
        log.info("【取消售后】订单id：" + orderId);
        String redisKey = "h5_oms_order_cancelRefund_" + orderId;
        String redisValue = orderId + "_" + System.currentTimeMillis();
        MemberVo member = (MemberVo) LocalDataUtil.getVar(Constants.MEMBER_INFO);
        try{
            RedisExUtils.lock(redisKey, redisValue, 60);
            return R.ok(aftersaleService.cancelRefund(orderId, member.getId()));
        }catch (Exception e){
            log.error("取消售后发生异常",e);
            throw new RuntimeException(e.getMessage());
        }finally {
            try {
                RedisExUtils.unLock(redisKey, redisValue);
            } catch (Exception e) {
                log.error("", e);
            }
        }
    }

    /***
     * 售后订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/refundOrderDetail")
    public R<AftersaleVo> refundOrderDetail(@RequestParam Long orderId){
        MemberVo member = (MemberVo) LocalDataUtil.getVar(Constants.MEMBER_INFO);
        return R.ok(aftersaleService.refundOrderDetail(orderId, member.getId()));
    }
}
